

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GUI extends JFrame implements MouseListener {
    public Chess chess;


    public GUI(String title, Chess chess) {
        super(title);
        this.chess = chess;
        this.setSize(510, 570);
        this.setLocation(800, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.addMouseListener(this);

    }

    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);

        // 横
        for (int i = 1; i < 16; i++) {
            g.drawString(String.format("%2d", i), 30 * i - 5, 45);
            g.drawString(String.format("%2d", i), 10, 30 * i + 35);
            g.drawLine(30, 30 + i * 30, 450, 30 + i * 30);
        }
        // 竖线
        for (int i = 0; i < 15; i++) {
            g.drawLine(30 + i * 30, 60, 30 + i * 30, 480);
        }
        int[][] board = chess.Edge;
        for (int i = 1; i <= 15; i++) {
            for (int j = 1; j <= 15; j++) {
                if (board[j][i] == 2)
                    g.drawOval(i * 30 - 10, 20 + j * 30, 20, 20);
                if (board[j][i] == 1)
                    g.fillOval(i * 30 - 10, 20 + j * 30, 20, 20);
            }
        }
        int []r = chess.evaALL();
        int bl = r[1];
        int wh = r[2];
        double bWin = (double) bl / (double) (bl + wh);
        double wWin = (double) wh / (double) (bl + wh);
        g.drawString(String.format("黑色得分%7d胜率%4.4f",bl,bWin), 89,510 );
        g.drawString(String.format("白色得分%7d胜率%4.4f",wh,wWin),89,530);
        g.drawString(String.format("Create By LiHan  QQ:253514942"),89,550);
    }

    //鼠标监听事件
    @Override
    public void mousePressed(MouseEvent e) {

        //获取棋盘上的位置
        int col = (e.getX() - 15) / 30 + 1;
        int row = (e.getY() - 45) / 30 + 1;
        //System.out.println(row+"|"+col);
        if (!(chess.getE(row, col) == 0 && col >= 1 && col <= 15 && row >= 1 && row <= 15)) {
            return;
        }

        chess.putChess(row, col);
        this.repaint();
        //判断输赢
        int WinOrLose = chess.WinOrLose(row, col);
        if (WinOrLose == 1) {
            System.out.println("BLACK WIN!");
            JOptionPane.showMessageDialog(null,
                    "黑的赢了",
                    "恭喜",
                    JOptionPane.DEFAULT_OPTION);
            System.exit(0);
        } else if (WinOrLose == 2) {
            System.out.println("WHITE WIN!");
            JOptionPane.showMessageDialog(null,
                    "白的赢了",
                    "恭喜",
                    JOptionPane.DEFAULT_OPTION);
            System.exit(0);
        }
        //电脑下
        int[] res = chess.evaluate();
        System.out.printf("%d %d |得分： %d\n", res[0], res[1], res[2]);
        chess.putChess(res[0], res[1]);
        this.repaint();
        WinOrLose = chess.WinOrLose(res[0], res[1]);
        if (WinOrLose == 1) {
            System.out.println("BLACK WIN!");
            JOptionPane.showMessageDialog(null,
                    "黑的赢了",
                    "恭喜",
                    JOptionPane.DEFAULT_OPTION);
            System.exit(0);
        } else if (WinOrLose == 2) {
            System.out.println("WHITE WIN!");
            JOptionPane.showMessageDialog(null,
                    "白的赢了",
                    "恭喜",
                    JOptionPane.DEFAULT_OPTION);
            System.exit(0);
        }

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}